package KK.System
{ 
	
	import KK.EMVC.Control.Control;
	import KK.GameBase.Events.SceneEvent;
	import KK.GameBase.Events.SceneIndexEvent;
	import KK.GameBase.IEventBase;
	import KK.GameBase.IMessageFollowers;
	import KK.GameBase.IMessageQueue;
	import KK.GameBase.MSGEvent;
	import KK.GameBase.SpriteBase;
	import KK.System.System;
	import KK.System.events.DebugEvent;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.system.ApplicationDomain;
	import flash.ui.Keyboard;
	
	public class Debug extends Control implements IMessageFollowers , IEventBase, IDebug
	{
		static protected var _event:IMessageQueue;
		static protected var child:MovieClip;
		
		static protected var cache:String;
		
		
		static private const C_W_DEBUG:String="Debug";//域名称
		
		//////////////////////////////////////////////////
		static private var AUTO_INCREMENT:int=0;
		static private function get AI():int{
			return AUTO_INCREMENT++;
		}
		//////////////////////////////////
		static public const MIN:int=AI;//最小状态
		static public const NORMAL:int=AI;//正常状态
		static public const COMMAND:int=AI;//显示命令行
		
		
		
		
		public function Debug()
		{
			init();
		}
		private function init():void{
			cache='';
		}
		
		/**
		 * 加载debug ui 
		 * @param event
		 * 
		 */
		protected function SetApp(application:ApplicationDomain):void
		{
			
			child=MovieClip(new (application.getDefinition(C_W_DEBUG)));
			_event.Publish(new SceneEvent(SceneEvent.ADD_SCENE_CHILD_EVENT,child,SpriteBase.POP));
			
			child.title.close.addEventListener(MouseEvent.MOUSE_DOWN,MouseClick);
			if(child.stage){
				initStage();
			}else{
				child.addEventListener(Event.ADDED_TO_STAGE,initStage);
			}
			
		} 
		
		private function initStage(event:Event=null):void{
			child.stage.addEventListener(KeyboardEvent.KEY_DOWN,EShortCuts);
			child.ShowMin();
		}
		protected function EShortCuts(event:KeyboardEvent):void{
		}
		override public function BindEvent(_event:IMessageQueue):Boolean{
			var result:Boolean=super.BindEvent(_event);
			Debug._event=_event;
			return result;
		} 
		
		protected function MouseClick(event:MouseEvent):void{
			
		}
		
		/**
		 *  显示debug消息 
		 * @param content
		 * 
		 */
		static public function AddMSG(content:String):void
		{
			if(child)
			{
				child.AddMSG(content);
				
				cache+=content+"\r\n";
				_event.Publish(new SceneIndexEvent(SceneIndexEvent.SET_SCENE_INDEX_EVENT,child,SpriteBase.POP));
			}
		}
		
		
		/**
		 * 获得输入命令  
		 * @return 
		 * 
		 */
		public function get Command():String{
			var result:String = child.inputComm.text;
			child.inputComm.text="";
			return result;
		}
	}
}